import mysql.connector
import matplotlib.pyplot as plt
from matplotlib.pyplot import MultipleLocator
from datetime import datetime

x1 = []
y1 = []

conn = mysql.connector.connect(user='root', password='123456', host='localhost', port='3306',
                               database='spider', use_unicode=True, auth_plugin='mysql_native_password')
c = conn.cursor()
c.execute("select FSRQ, LJJZ from nuoan_hunhe order by FSRQ")
# c.execute('select FSRQ, LJJZ from river')
for row in c:
	x1.append(datetime.strptime(row[0], "%Y-%m-%d"))
	y1.append(float(row[1]))
c.close()
conn.close()

print(len(x1))
print(len(y1))

# l1 = plt.plot(x1, y1, 'r--', label='type1')
# plt.plot(x1, y1, 'ro-')
# plt.title('The Lasers in Three Conditions')
# plt.xlabel('row')
# plt.ylabel('column')
# plt.legend()
# plt.show()

# 绘制出图形，大小是 (15,15)
fig = plt.figure(figsize=(20, 15))
# 绘制出图形    x轴     y轴
plt.plot(x1, y1)
# 设置x和y轴的文字颜色
plt.xticks(size=10, color='red')
plt.yticks(size=10, color='blue')
# 设置x轴和y轴的标签
plt.xlabel('年份', size=12, color='black', fontproperties='SimHei')
fig.autofmt_xdate()
plt.ylabel('累计净值', size=12, color='black', fontproperties='SimHei')
# 设置标题
plt.title('诺安成长混合', size=20, color='black', fontproperties='SimHei')
# 使用风格 bmh
plt.style.use('bmh')

# 把y轴的刻度间隔设置为0.1，并存在变量里
y_major_locator = MultipleLocator(0.1)
# ax为两条坐标轴的实例
ax = plt.gca()
# 把y轴的主刻度设置为10的倍数
ax.yaxis.set_major_locator(y_major_locator)
# 把y轴的刻度范围设置为-5到110，同理，-5不会标出来，但是能看到一点空白
plt.ylim(0.7, 3)

# 绘制出统计图
plt.show()
